<template>
	<!-- 试卷详情页 -->
	<view class="paper">
		<view class="title">{{paperInfo.paper}}</view>
		<view class="opt"><text>选择题</text> <text class="t">{{paperInfo.a}}</text></view>
		<view class="opt"><text>判断题</text> <text class="t">{{paperInfo.b}}</text></view>
		<view class="opt"><text>填空题</text> <text class="t">{{paperInfo.c}}</text></view>
		<view class="opt"><text>简答题</text> <text class="t">{{paperInfo.d}}</text></view>
		<view class="item">满分: {{paperInfo.total}}分</view>
		<view class="item">及格: {{paperInfo.passline}}分</view>
		<view class="item">时长: {{paperInfo.time}}小时</view>
		<view class="button" @click="jumpToExamFn">进入考试</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				paperInfo: {
					paper: "", //试卷名称
					a: 0,
					b: 0,
					c: 0,
					d: 0,
					total: 0, //总分
					passline: 0, //及格线
					time: 0, //时长
					_id: "", // 试卷唯一的标记
				}
			}
		},
		methods: {
			async getPaperInfoFn(name, _id) {
				uni.showLoading({
					title: "加载试卷信息"
				})
				let that = this;
				await uniCloud.callFunction({
						name: "paper",
						data: {
							name: name
						}
					})
					.then(
						resData => {
							if (resData.result && resData.result.data) {
								let data = resData.result.data;
								// console.log(data);
								if (data.length != 0) {
									// 挑选了试卷
									let obj = data.find(item => item._id == _id);
									// console.log("试卷信息:", obj);
									that.paperInfo.paper = obj.paper
									that.paperInfo.total = obj.total
									that.paperInfo.passline = obj.passline
									that.paperInfo.time = obj.time
									that.paperInfo._id = obj._id;
									let topics = obj.topics;
									let a = 0,
										b = 0,
										c = 0,
										d = 0;
									for (let i = 0; i < topics.length; i++) {
										if (topics[i].type == 'a') {
											a += 1;
										}
										if (topics[i].type == 'b') {
											b += 1;
										}
										if (topics[i].type == 'c') {
											c += 1;
										}
										if (topics[i].type == 'd') {
											d += 1;
										}
									}
									that.paperInfo.a = a;
									that.paperInfo.b = b;
									that.paperInfo.c = c;
									that.paperInfo.d = d;
								}
							}
						}
					)
					.catch(err => {
						console.log(err)
						uni.hideLoading();
					})

				uni.hideLoading();
			},
			jumpToExamFn() {
				if (this.paperInfo._id) {
					uni.redirectTo({
						url: `/pages/exam/exam?_id=${this.paperInfo._id}`
					})
				} else {
					uni.showToast({
						title: "试卷尚未获取",
						icon: "none"
					})
				}
			}
		},
		onLoad(options) {
			if (options._id) {
				let _id = options._id;
				let name = options.name;
				this.getPaperInfoFn(name, _id);
			}
		}
	}
</script>





<style lang="scss">
	@import "../../static/scss/variable.scss";

	.paper {
		padding: $padd;

		.opt {
			padding: 16rpx 0rpx;
			border-bottom: 1px solid #f0f0f0;

			.t {
				font-size: 36rpx;
				// font-weight: bold;
				margin-left: 10rpx;
				text-decoration: underline;
			}
		}

		.item {
			padding: 16rpx 0rpx;
			font-weight: bold;
			border-bottom: 1px solid #f0f0f0;
		}

		.button {
			border: 3rpx solid $theme-color;
			display: inline-block;
			margin: 10rpx 0rpx;
			padding: 6rpx 20rpx;
			border-radius: 10rpx;
			color: $theme-color;
		}
	}
</style>